package com.example.d6_homework.homework02;

import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        // 需求：创建4个学生对象,存放到集合中,遍历集合,将名字为张三丰的学生地址修改成"山东"
        // 1.创建ArrayList集合
        List<Student> list = new ArrayList<>();

        // 2.创建4个对象
        list.add(new Student(1, "张无忌", "河北"));
        list.add(new Student(2, "张三丰", "河南"));
        list.add(new Student(3, "赵敏", "蒙古"));
        list.add(new Student(4, "周芷若", "四川"));

        // 使用stream流一行代码直接实现
        // peek方法是对流中的每个元素执行某个操作,但是不会改变流中元素的本身
        list.stream().peek(student -> {
            if (student.getName().equals("张三丰")) {
                student.setAddress("山东");
            }
        }).forEach(student -> System.out.println("学生学号为:" + student.getId() + ",姓名为:" + student.getName() + ",住址为:" + student.getAddress()));


        // 3.遍历集合中的数据
        for (Student student : list) {
            if (student.getName().equals("张三丰")) {
                student.setAddress("山东");
            }
        }

        // 4.遍历集合中的数据
        list.forEach(student -> System.out.println("学生学号为:" + student.getId() + ",姓名为:" + student.getName() + ",住址为:" + student.getAddress()));
    }
}
